package com.sr.examination.exam;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;

import com.sr.examination.R;
import com.sr.examination.exam.common.PayModel;
import com.sr.examination.exam.common.adapter.ExamAdapter;
import com.sr.examination.exam.common.widget.ExamMorePop;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.event.EventUtil;
import com.tl.commonlibrary.event.LoginStatusEvent;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.DeviceUtil;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.beans.ExamBean;
import com.tl.commonlibrary.ui.beans.ExamCategory;
import com.tl.commonlibrary.ui.beans.ExamSubBean;
import com.tl.commonlibrary.ui.user.Account;
import com.tl.commonlibrary.ui.widget.TitleBar;
import com.tl.commonlibrary.ui.widget.pull.PullToRefreshLayout;
import com.tl.commonlibrary.ui.widget.pull.PullableListView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import retrofit2.Call;

/**
 * Created by jiangzhongshan on 18/2/3.
 * 试卷列表页
 */

public class ExamListActivity extends BaseFragmentActivity implements PullToRefreshLayout.OnRefreshListener, PayModel.ExamPurchaseListener {
    public static void start(Context context, ExamCategory category) {
        Intent starter = new Intent(context, ExamListActivity.class);
        starter.putExtra("category", category);
        context.startActivity(starter);
    }

    private ExamCategory category;
    private PullableListView listView;
    private PullToRefreshLayout controller;
    private ArrayList<ExamBean> datas;
    private ExamAdapter adapter;
    private int page = 1;

    private PayModel payModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_exam_list);
        EventUtil.register(this);
        category = (ExamCategory) getIntent().getSerializableExtra("category");
        setTitle(category.getTitle());
        controller = findViewById(R.id.listViewController);
        listView = findViewById(R.id.listView);
        listView.setClipToPadding(false);
        int padding = context.getResources().getDimensionPixelOffset(R.dimen.spacing_30px);
        listView.setPadding(0, padding, 0, padding);
        listView.setDivider(new ColorDrawable(ContextCompat.getColor(context, R.color.transparent)));
        listView.setDividerHeight((int) getResources().getDimension(R.dimen.base_spacing_20px));
        controller.setOnRefreshListener(this);
        controller.postDelayed(new Runnable() {
            @Override
            public void run() {
                controller.autoRefresh();
            }
        }, 300);

        payModel = new PayModel((BaseFragmentActivity) context);
        payModel.setListener(this);
    }

    private void getExamList() {
        dismissNoData();
        Net.getExamList(Account.getLevelId(), category.getClassId(), page, new RequestListener<BaseBean<ArrayList<ExamBean>>>() {
            @Override
            public void onSuccess(Call<BaseBean<ArrayList<ExamBean>>> call, BaseBean<ArrayList<ExamBean>> response) {
                if (response.isSuccessful()) {
                    if (response.data != null && !response.data.isEmpty()) {
                        if (page == 1) {
                            datas = response.data;
                            adapter = new ExamAdapter(context, datas);
                            adapter.setPayModel(payModel);
                            listView.setAdapter(adapter);
                        } else {
                            datas.addAll(response.data);
                            adapter.notifyDataSetChanged();
                        }
                        page++;
                        controller.finish(PullToRefreshLayout.SUCCEED);
                    } else {
                        if (page == 1) {
                            showNoDataView();
                        } else {
                            UIUtils.showToast(getString(R.string.all_data_loaded));
                        }
                        controller.finish(PullToRefreshLayout.FAIL);
                    }
                }
            }

            @Override
            public void onFailed(Call<BaseBean<ArrayList<ExamBean>>> call, ErrorResponse errorResponse) {
                controller.finish(PullToRefreshLayout.FAIL);
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(LoginStatusEvent event) {
        if (listView != null) {
            listView.setAdapter(null);
            if (controller != null) {
                controller.autoRefresh();
            }
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout, boolean isAuto) {
        page = 1;
        getExamList();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout, boolean isAuto) {
        getExamList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (payModel != null) {
            payModel.onDestroy();
            payModel = null;
        }
    }

    @Override
    public void purchaseSuccess() {
        if (listView != null) {
            listView.setAdapter(null);
            if (controller != null) {
                controller.autoRefresh();
            }
        }
    }
}
